package controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import model.CustomerList;
import model.InventoryList;
import model.OrderList;

/***********************************************************
 * Class: OrderController
 * @author jdsmith
 * description: facilitates writing orders to disk and
 * instantiating existing orders from disk.
 **********************************************************/
public class OrderController {
	/**
	 * constructor
	 */
	public OrderController(){}
	
	/**
	 * initOrders
	 * @param a filepath to the saved file
	 * @return a list of orders
	 * @throws FileNotFoundException
	 *description: reads in a file containing an order list
	 *and instantiates an order list.
	 */
	public OrderList initOrders(String path) throws FileNotFoundException{
		OrderList list = new OrderList();
		
		//open the file and read in the order list
		FileInputStream fis = new FileInputStream(path);
	       try {
			ObjectInputStream ois = new ObjectInputStream(fis);
			
			list = (OrderList) ois.readObject();
			ois.close();
		} catch (Exception e) {
			//Some error occurred when reading in the stream,
			//so just ignore it and return an empty list.
		}
		return list;
	}//end initOrders
	
	/**
	 * initCustomers
	 * @param the filepath to the save file
	 * @return a list of Customers
	 * @throws FileNotFoundException
	 * description: reads in a file containing a customer list
	 * and returns it as a customerlist object.
	 */
	public CustomerList initCustomers(String path) throws FileNotFoundException{
		CustomerList list = new CustomerList();

		//open the file and read in the order list
		FileInputStream fis = new FileInputStream(path);
		try {
			ObjectInputStream ois = new ObjectInputStream(fis);

			list = (CustomerList) ois.readObject();
			ois.close();
		} catch (Exception e) {
			//Some error occurred when reading in the stream,
			//so just ignore it and return an empty list.
		}
		return list;
	}//end initCustomers
	
	
	/**
	 * initInventory
	 * @param the filepath to the save file
	 * @return a list of Items
	 * @throws FileNotFoundException
	 * description: reads in a file containing a customer list
	 * and returns it as an InventoryList object.
	 */
	public InventoryList initInventory(String path) throws FileNotFoundException{
		InventoryList list = new InventoryList();

		//open the file and read in the order list
		FileInputStream fis = new FileInputStream(path);
		try {
			ObjectInputStream ois = new ObjectInputStream(fis);

			list = (InventoryList) ois.readObject();
			ois.close();
		} catch (Exception e) {
			//Some error occurred when reading in the stream,
			//so just ignore it and return an empty list.
		}
		return list;
	}//end initInventory
	
	/**
	 * save
	 * @param a serializable object
	 * @param the filepath to a save file
	 * @throws FileNotFoundException
	 * @throws IOException
	 * 
	 * description: writes a serializable object to the specified file.
	 */
	public void save(Serializable item, String path) throws FileNotFoundException, IOException{
		
		FileOutputStream fos = new FileOutputStream(path);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(item);
		oos.close();
	}//end save
	
}
